package mytest.util;

import static org.testng.Assert.fail;
import java.io.IOException;
import java.net.URI;

import org.apache.http.client.HttpResponseException;

import com.offbytwo.jenkins.model.Build;
import com.offbytwo.jenkins.model.ConsoleLog;
import com.offbytwo.jenkins.model.Job;
import test.service.ServiceJenkins;
import test.testcase.jenkins;

public class JenkinsServer {

	private com.offbytwo.jenkins.JenkinsServer server;
	private String url;
	private String username;
	private String password;
	private String jobName;
	private Job job;
	private String folder;
	private String projectid;

	public JenkinsServer(String projectid) {
		setProjectid(projectid);
		jenkins jenkins = ServiceJenkins.getJenkinsInfo(projectid);
		if (jenkins == null) {
			return;
		}
		folder = jenkins.getFolder();
		jobName = jenkins.getJobName();
		url = jenkins.getHost();
		password = jenkins.getToken();
		username = jenkins.getUsername();
		
		if (server == null) {
			try {
				server = new com.offbytwo.jenkins.JenkinsServer(new URI(url), username, password);
				if (folder.equals("")) {
					job = server.getJob(jobName);
				}else {
					job = server.getFolderJob(server.getJobs().get(folder)).get().getJobs().get(jobName);
				}
			} catch (Exception e) {
				e.printStackTrace();
				fail();
			}
		}
	}
	
	public boolean isBuildding() {
		try {
			return getLastBuild().details().isBuilding();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean isBuildding(int buildNumber) {
		try {
			return job.details().getBuildByNumber(buildNumber).details().isBuilding();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public Job getJob() {
		return job;
	}
	
	public void stopBuild(String buildNumber) {
		try {
			getJob().details().getBuildByNumber(Integer.valueOf(buildNumber)).Stop();
		} catch (HttpResponseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String build() {
		String startNumber;
		if (getJob() == null) {
			return null;
		}
		try {
			startNumber = getLastBuildNumber();
			int time = 15;  //等待15秒
			int start = 0;
			getJob().build(true);
			boolean condition = true;
			while(condition) {
				if (start>=time) {
					break;
				}
				String thisBuildNumber = getLastBuildNumber();
				if (!thisBuildNumber.equals(startNumber)) {
					return thisBuildNumber;
				}
				Thread.sleep(1000);
				start++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Build getLastBuild() {
		try {
			if (getJob() == null) {
				return null;
			}
			return getJob().details().getLastBuild();
		} catch (IOException e) {
			return null;
		}
	}
	
	public String getLastBuildStatus() {
		try {
			return getLastBuild().details().getResult().toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String getLastBuildNumber() {
		if (getLastBuild() == null) {
			return null;
		}
		try {
			if (getLastBuild() == null) {
				return null;
			}
			return getLastBuild().details().getNumber()+"";
		} catch (IOException e) {
			return null;
		}
		
	}
	
	public String getLastBuildTime() {
		try {
			return getLastBuild().details().getDuration()/1000+"";
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public ConsoleLog getLastBuildConsole(int bufferOffset) {
		try {
			return getLastBuild().details().getConsoleOutputText(bufferOffset);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getJobName() {
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	public String getProjectid() {
		return projectid;
	}

	public void setProjectid(String projectid) {
		this.projectid = projectid;
	}
	
}
